import dayjs, { Dayjs } from "dayjs"

export type TimeRange = [Dayjs, Dayjs]

export const rangeToday = (): TimeRange => [dayjs().startOf("day"), dayjs().endOf("day")]

export const rangeTomorrow = (): TimeRange => [dayjs().startOf("day").add(1, "d"), dayjs().endOf("day").add(1, "d")]

export const rangeYesterday = (): TimeRange => [dayjs().startOf("day").subtract(1, "d"), dayjs().endOf("day").subtract(1, "d")]

export const rangePastNDays = (n: number): TimeRange => [
	dayjs()
		.startOf("day")
		.subtract(n - 1, "day"),
	dayjs().endOf("day")
]

export const rangePastNMonth = (n: number): TimeRange => [
	dayjs().subtract(n, "month").startOf("month"),
	dayjs().subtract(n, "month").endOf("month")
]

export const rangeLastNMonth = (n: number): TimeRange => [
	dayjs().subtract(n, "month").startOf("month"),
	dayjs().subtract(1, "month").endOf("month")
]
export const rangeLastMonthN = (n: number): TimeRange => [dayjs().subtract(n, "month"), dayjs()]

export const rangeCurrentMonth = (): TimeRange => [dayjs().startOf("month"), dayjs().endOf("day")]

export const rangePastWeek = (): TimeRange => [dayjs().startOf("day").subtract(6, "day"), dayjs().endOf("day")]

export const rangeLastMonth = (): TimeRange => [
	dayjs().startOf("month").subtract(1, "month"),
	dayjs().endOf("month").subtract(1, "month").add(1, "day")
]

const calculateLocaleDiff = () => (dayjs.locale() === "en" && !dayjs().day() ? 1 : 0)

export const rangeLastWeek = (): TimeRange => [
	dayjs()
		.subtract(1 + calculateLocaleDiff(), "week")
		.startOf("week")
		.add(calculateLocaleDiff(), "d"),
	dayjs()
		.subtract(1 - calculateLocaleDiff(), "week")
		.endOf("week")
		.subtract(-calculateLocaleDiff(), "day")
]

export const rangeCurrentWeek = (): TimeRange => [
	dayjs().startOf("day").startOf("week").add(calculateLocaleDiff(), "d"),
	dayjs().endOf("day").add(calculateLocaleDiff(), "day")
]

export const rangeBeginning = (): TimeRange => {
	const end = dayjs().endOf("day")
	const start = dayjs("20231115", "YYYYMMDD").startOf("day")
	return [start, end]
}
interface FormatObject {
	y: number
	m: number
	d: number
	h: number
	i: number
	s: number
	a: number
}

export function parseTime(time: string | number | Date, cFormat?: string): string | null {
	if (arguments.length === 0) {
		return null
	}

	const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}"
	let date: Date

	if (typeof time === "object") {
		date = time as Date
	} else {
		if (typeof time === "string" && /^[0-9]+$/.test(time)) {
			time = parseInt(time)
		}
		if (typeof time === "number" && time.toString().length === 10) {
			time = time * 1000
		}
		date = new Date(time)
	}

	if (!date) return ""

	const formatObj: FormatObject = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}

	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: string, key: string) => {
		let value: string | number = formatObj[key as keyof FormatObject]
		// Note: getDay() returns 0 on Sunday
		// if (key === "a") {
		// 	return [
		// 		i18n.t("common.utils.day"),
		// 		i18n.t("common.utils.one"),
		// 		i18n.t("common.utils.two"),
		// 		i18n.t("common.utils.three"),
		// 		i18n.t("common.utils.four"),
		// 		i18n.t("common.utils.five"),
		// 		i18n.t("common.utils.six")
		// 	][value]
		// }
		if (result.length > 0 && value < 10) {
			value = "0" + value
		}
		return value.toString() || "0"
	})

	return time_str
}

export function timeTurner(time: number) {
	let day = Math.floor(time / 86400)

	time = time % 86400
	let hour = Math.floor(time / 3600)
	time %= 3600
	let minute = Math.floor(time / 60)
	time %= 60

	let str = `${day > 0 ? day + "d" : ""}${hour > 0 || day ? hour + "h" : ""}${minute > 0 || hour ? minute + "m" : ""}${time}s`

	return str
}
